package com.buaa.aimall.goodsModule.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buaa.aimall.Utils.BaseResponse;
import com.buaa.aimall.Utils.Enum.StatusCode;
import com.buaa.aimall.Utils.PageReturnResult;
import com.buaa.aimall.goodsModule.DTO.DistributeCouponDTO;
import com.buaa.aimall.goodsModule.DTO.FilterCouponDTO;
import com.buaa.aimall.goodsModule.Entity.CouponEntity;
import com.buaa.aimall.goodsModule.Service.CouponService;
import com.buaa.aimall.userModule.Entity.UserEntity;
import com.buaa.aimall.userModule.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/goods/coupon")
@CrossOrigin
public class CouponController {
    @Autowired
    UserService userService;

    @Autowired
    CouponService couponService;

    @PostMapping("/distribute-coupons")
    public BaseResponse<Boolean> distributeCoupons(@RequestBody DistributeCouponDTO distributeCouponDTO) {
        UserEntity user = userService.getById(distributeCouponDTO.getUser());
        if (user == null) {
            return new BaseResponse<>(StatusCode.DAO_ERROR, "用户不存在", null);
        }
        if (!(distributeCouponDTO.getDiscount() >= 0 && distributeCouponDTO.getDiscount() < 100)) {
            return new BaseResponse<>(StatusCode.PARAM_ERROR, "折扣参数discount不合法，应为[0, 100)", null);
        }
        if (distributeCouponDTO.getValidityDays() <= 0) {
            return new BaseResponse<>(StatusCode.PARAM_ERROR, "有效期参数不合法，至少为1", null);
        }

        var coupon = CouponEntity.builder()
                .user(distributeCouponDTO.getUser())
                .discount(distributeCouponDTO.getDiscount())
                .expirationTime(LocalDateTime.now().plusDays(distributeCouponDTO.getValidityDays()))
                .usageTimes(distributeCouponDTO.getUsageTimes())
                .usefor(distributeCouponDTO.getUsefor())
                .externalPlatform(distributeCouponDTO.getExternalPlatform())
                .build();
        var result = couponService.save(coupon);
        return new BaseResponse<>("发放优惠券", result, null);
    }

    @PostMapping("/filter-coupons")
    public BaseResponse<PageReturnResult<CouponEntity>> filterCoupons(@RequestBody FilterCouponDTO filterCouponDTO) {
        UserEntity user = userService.getCurrentUser();
        var result = couponService.getPageByUser(user.getId(), filterCouponDTO.getPageSize(), filterCouponDTO.getCurrentPage(), filterCouponDTO.getUsefor());
        return new BaseResponse<>(StatusCode.SUCCESS, "获取当前用户优惠券成功", result);
    }

    @PostMapping("get-coupon-distribution-records")
    public BaseResponse<Page<CouponEntity>> getDistributionRecords(@RequestBody FilterCouponDTO request) {
        Page<CouponEntity> page = new Page<>(request.getCurrentPage(), request.getPageSize());
        LambdaQueryWrapper<CouponEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (request.getUsefor() != null) {
            queryWrapper.eq(CouponEntity::getUsefor, request.getUsefor());
        }
        Page<CouponEntity> entityPage = couponService.page(page, queryWrapper);
        return new BaseResponse<>(StatusCode.SUCCESS, "获取优惠券发放记录成功", entityPage);
    }
}
